package com.source.thread.threadd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Package file
 * @ClassName FileThread.java
 * @date 2019年4月9日 下午5:14:44
 * @version V1.0
*/
public class FileThread {

    public static void main(String[] args) throws Exception {
        long t1 = System.currentTimeMillis();

        // String localFilePath = localTempPath+"/"+fileName;
        String localFilePath = "D:\\000\\ConsulUserSvc\\src\\main\\java\\com\\yq\\controller\\SvcInfoController.java";

        // 开启固定线程池
        ExecutorService exec = Executors.newFixedThreadPool(50);
        // 逐行读取本地文件
        List<String> dataList = new ArrayList<>();

        // File f = new File("D:\\a\\b\\in.txt");
        File f = new File(localFilePath);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(f), "utf-8");
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        // 定义计数器
        int i = 0;
        while ((str = br.readLine()) != null) {
            // i的值是从1开始
            i++;
           System.out.println(str);
            // 加入集合
            dataList.add(str);
            if (i % 10 == 0) {
                System.out.println("i........................................"+i);
                // 每次传入线程的集合
                List<String> onceList = new ArrayList<>();
                for (String item : dataList) {
                    onceList.add(item);
                }
                // 清空集合
                dataList = null;
                // 重构集合
                dataList = new ArrayList<String>();

                Map<String, Object> pMap = new HashMap<>();
                // 开启线程
                Runnable task = new BatchHandlerThreadTask(onceList, pMap);
                exec.submit(task);
            }
        }
        reader.close();
        br.close();

        // 判断最后一次
        if (dataList.size() != 0) {
            Map<String, Object> pMap = new HashMap<>();
            Runnable task = new BatchHandlerThreadTask(dataList, pMap);
            exec.submit(task);
        }

        exec.shutdown();
        while (true) {
    //        System.out.println("..."+exec.isTerminated());
            if (exec.isTerminated()) {
                System.out.println("全部线程都结束了，i: "+i+"耗时："+(System.currentTimeMillis()-t1));
                break;
            }
        }

    }


}
